/**
 *
 */
package cn.hg.jfsa.manage.controller;

import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.jfsa.manage.web.LayuiPage;
import cn.hg.jfsa.web.theme.ThemeManager;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * @author Golly
 *
 */
public class DashboardController extends BaseController {

	/*
	 * （非 Javadoc）
	 *
	 * @see cn.hg.base.controller.BaseController#index()
	 */
	@Override
	public void index() {

		this.setAttr("sysProp", System.getProperties());
	}

	public void ajaxGetNewsRanking() {
		Integer pageIndex = this.getInt("page", 1);
		Integer pageSize = this.getInt("limit", 10);

		Page<Record> page = Db.paginateByCache("news", "dashboard_news_" + pageIndex.toString() + "_"
						+ pageSize.toString(), pageIndex, pageSize, "SELECT n.title, nc.`name` AS categoryName, n.hits, a.`name` AS editorName, n.img, n.edited_time AS editedTime",
				"FROM t_news AS n LEFT JOIN t_news_category AS nc ON n.category_id = nc.id LEFT JOIN jfsa_admin AS a ON n.editor_id = a.id WHERE n.theme = ? AND n.`status` = 'on' ORDER BY n.hits DESC, editedTime DESC",
				ThemeManager.me().getCurrentTheme().getId());

		this.renderJson(new LayuiPage(page));
	}

	public void ajaxGetDocRanking() {
		Integer pageIndex = this.getInt("page", 1);
		Integer pageSize = this.getInt("limit", 10);

		Page<Record> page = Db.paginateByCache("doc", "dashboard_doc_" + pageIndex.toString() + "_"
						+ pageSize.toString(), pageIndex, pageSize, "SELECT d.title, dc.`name` AS categoryName, d.hits, a.`name` AS editorName, d.edited_time AS editedTime, d.ad_img AS img",
				"FROM t_doc AS d LEFT JOIN t_doc_category AS dc ON d.category_id = dc.id LEFT JOIN jfsa_admin AS a ON d.editor_id = a.id WHERE d.theme = ? AND d.`status` = 'on' ORDER BY d.hits DESC, editedTime DESC",
				ThemeManager.me().getCurrentTheme().getId());

		this.renderJson(new LayuiPage(page));
	}

	public void ajaxGetGoodsRanking() {


	}

	/**
	 * 登录日志表，按Page进行查询
	 */
	public void ajaxGetLogLoginList(){
		Integer pageIndex = this.getInt("page", 1);
		Integer pageSize = this.getInt("limit", 10);

		Page<Record> page = Db.paginateByCache("log", "dashboard_log_login_" + pageIndex.toString() + "_"
						+ pageSize.toString(), pageIndex, pageSize, "SELECT ll.id, ll.admin_id AS adminId, ll.login_time AS loginTime, ll.login_ip AS loginIp, a.`name`",
				"FROM jfsa_log_login AS ll LEFT JOIN jfsa_admin AS a ON ll.admin_id = a.id ORDER BY ll.login_time DESC");

		this.renderJson(new LayuiPage(page));
	}
}
